package com.qaml.mes.device.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.earth.user.AuthSsoContext;
import com.qaml.mes.device.dao.CheckUpkeepUserDO;
import com.qaml.mes.device.dao.mapper.CheckUpkeepUserMapper;
import com.qaml.mes.device.repository.CheckUpkeepUserRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName CheckUpkeepUserRepositoryImpl
 * @Description TODO
 * @Author leikai
 * @Date 2024/2/27 0027 9:53
 * @Version 1.0
 */
@Repository
public class CheckUpkeepUserRepositoryImpl implements CheckUpkeepUserRepository {

    @Resource
    private CheckUpkeepUserMapper checkUpkeepUserMapper;

    /**
     * 通过检保项目编号删除对应的人员
     *
     * @param checkUpkeepId
     */
    @Override
    public void deleteByCheckUpkeepId(String checkUpkeepId) {
        LambdaQueryWrapper<CheckUpkeepUserDO> deleteQueryWrapper = new LambdaQueryWrapper<>();
        deleteQueryWrapper.eq(CheckUpkeepUserDO::getIsDeleted, 0);
        deleteQueryWrapper.eq(CheckUpkeepUserDO::getCheckUpkeepId, checkUpkeepId);
        deleteQueryWrapper.eq(CheckUpkeepUserDO::getTenantId, AuthSsoContext.getInstance().getTenantId());
        checkUpkeepUserMapper.delete(deleteQueryWrapper);
    }

    @Override
    public void saveBatch(List<CheckUpkeepUserDO> checkUpkeepUserDOS) {
        checkUpkeepUserMapper.saveBatch(checkUpkeepUserDOS);
    }


    @Override
    public void saveBatchByFor(List<CheckUpkeepUserDO> checkUpkeepUserDOS) {
        if (!CollectionUtils.isEmpty(checkUpkeepUserDOS)) {
            for (CheckUpkeepUserDO checkUpkeepUserDO : checkUpkeepUserDOS) {
                checkUpkeepUserMapper.insert(checkUpkeepUserDO);
            }
        }
    }


    /**
     * 通过检保项目编号获取对应的人员
     *
     * @param checkUpkeepId
     * @return
     */
    @Override
    public List<CheckUpkeepUserDO> getByCheckUpkeepId(String checkUpkeepId) {

        LambdaQueryWrapper<CheckUpkeepUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepUserDO::getIsDeleted, 0);
        queryWrapper.eq(CheckUpkeepUserDO::getCheckUpkeepId, checkUpkeepId);
        queryWrapper.eq(CheckUpkeepUserDO::getTenantId, AuthSsoContext.getInstance().getTenantId());
        List<CheckUpkeepUserDO> checkUpkeepUserDOS = checkUpkeepUserMapper.selectList(queryWrapper);

        if (checkUpkeepUserDOS == null || checkUpkeepUserDOS.size() == 0) {
            return Collections.emptyList();
        }
        return checkUpkeepUserDOS;
    }


    @Override
    public List<CheckUpkeepUserDO> getByType(String userId, String type) {
        LambdaQueryWrapper<CheckUpkeepUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepUserDO::getIsDeleted, 0);
        queryWrapper.eq(CheckUpkeepUserDO::getUserId, userId);
        queryWrapper.eq(Objects.nonNull(type), CheckUpkeepUserDO::getPersonType, type);
        queryWrapper.eq(CheckUpkeepUserDO::getTenantId, AuthSsoContext.getInstance().getTenantId());
        return checkUpkeepUserMapper.selectList(queryWrapper);
    }

    @Override
    public List<CheckUpkeepUserDO> getByCheckUpkeepIds(List<String> checkUpkeepIds, String type) {
        LambdaQueryWrapper<CheckUpkeepUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepUserDO::getIsDeleted, 0);
        queryWrapper.in(CheckUpkeepUserDO::getCheckUpkeepId, checkUpkeepIds);
        queryWrapper.ne(StringUtils.isNotBlank(type), CheckUpkeepUserDO::getPersonType, type);
        queryWrapper.eq(CheckUpkeepUserDO::getTenantId, AuthSsoContext.getInstance().getTenantId());
        return checkUpkeepUserMapper.selectList(queryWrapper);
    }

    /**
     * 批量查询检保人员
     *
     * @param checkUpkeepIds
     * @param type
     * @return
     */
    @Override
    public List<CheckUpkeepUserDO> getCheckUserByCheckUpkeepIds(List<String> checkUpkeepIds, String type) {
        LambdaQueryWrapper<CheckUpkeepUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepUserDO::getIsDeleted, 0);
        queryWrapper.in(CheckUpkeepUserDO::getCheckUpkeepId, checkUpkeepIds);
        queryWrapper.eq(StringUtils.isNotBlank(type), CheckUpkeepUserDO::getPersonType, type);
        queryWrapper.eq(CheckUpkeepUserDO::getTenantId, AuthSsoContext.getInstance().getTenantId());
        return checkUpkeepUserMapper.selectList(queryWrapper);
    }


    @Override
    public List<CheckUpkeepUserDO> getByCheckUpkeepIdAndStatus(String checkUpkeepId, String type) {
        LambdaQueryWrapper<CheckUpkeepUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepUserDO::getIsDeleted, 0);
        queryWrapper.eq(CheckUpkeepUserDO::getCheckUpkeepId, checkUpkeepId);
        queryWrapper.eq(Objects.nonNull(type), CheckUpkeepUserDO::getPersonType, type);
        queryWrapper.eq(CheckUpkeepUserDO::getTenantId, AuthSsoContext.getInstance().getTenantId());
        return checkUpkeepUserMapper.selectList(queryWrapper);
    }

    /**
     * 接口 -->获取所有的复核(审核)人员
     *
     * @return
     */
    @Override
    public List<CheckUpkeepUserDO> getRecheckUserList(String personType) {
        return checkUpkeepUserMapper.getRecheckUserList(personType, AuthSsoContext.getInstance().getTenantId());
    }


    /**
     * 根据 checkUpkeepIds批量删除人员
     *
     * @return
     */
    @Override
    public void updateCheckUpkeepUserByIds(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return;
        }
        checkUpkeepUserMapper.updateCheckUpkeepUserByIds(ids, AuthSsoContext.getInstance().getTenantId());
    }

    @Override
    public void updateHolidayTipByIds(List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return;
        }
        checkUpkeepUserMapper.updateHolidayTipByIds(ids, AuthSsoContext.getInstance().getTenantId());
    }

    @Override
    public List<CheckUpkeepUserDO> getByCheckUpkeepIdAndStatusNoTenantId(String checkUpkeepId, String type) {
        LambdaQueryWrapper<CheckUpkeepUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepUserDO::getIsDeleted, 0);
        queryWrapper.eq(CheckUpkeepUserDO::getCheckUpkeepId, checkUpkeepId);
        queryWrapper.eq(Objects.nonNull(type), CheckUpkeepUserDO::getPersonType, type);
        return checkUpkeepUserMapper.selectList(queryWrapper);
    }

    @Override
    public void updateDataByCheckUpkeepId(CheckUpkeepUserDO checkUpkeepUserDO) {
        LambdaQueryWrapper<CheckUpkeepUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CheckUpkeepUserDO::getCheckUpkeepId, checkUpkeepUserDO);
        checkUpkeepUserMapper.update(checkUpkeepUserDO, queryWrapper);
    }


}